package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.pojo.Teachers;
import com.example.homeworkmanagement.service.TeachersService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/teachers")
@Api(tags="教师信息管理",description="提供管理API接口")
@CrossOrigin
public class TeachersController {
    @Autowired
    private TeachersService teachersService;
    @GetMapping("/login")
    @ApiOperation("教师登录")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "teacher_id",value = "教师工号id",required = true),
            @ApiImplicitParam(dataType = "string",name = "password",value = "密码",required = true),
    })
    public ResultVO<Teachers> login(String teacher_id, String password) throws Exception {
        return teachersService.login(teacher_id,password);
    }

    /**
     * 获取所有教师列表
     */
    @PostMapping("/getTeacherList")
    @ApiOperation(value = "获取所有教师列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师工号", required = false),
            @ApiImplicitParam(dataType = "string", name = "teacherName", value = "教师姓名", required = false),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "所属院系", required = false),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = false),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = false),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码", required = false),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = false)
    })
    public ResultVO<List<Teachers>> getTeacherList(Teachers teacher, Integer page, Integer size) {
        System.out.println("接收参数 - teacher: " + teacher);
        System.out.println("接收参数 - page: " + page + ", size: " + size);

        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }

        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return teachersService.getTeacherList(teacher,page,size);
    }

    /**
     * 根据工号获取教师详情
     */
    @GetMapping("/getTeacherDetail")
    @ApiOperation(value = "根据工号获取教师详情")
    @ApiImplicitParam(name = "teacherId", value = "教师工号", required = true, dataType = "string")
    public ResultVO<Teachers> getTeacherDetail(String teacherId) {
        return teachersService.getTeacherDetail(teacherId);
    }

    /**
     * 添加教师
     */
    @PostMapping("/addTeacher")
    @ApiOperation(value = "添加教师")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师工号（必须唯一）", required = true),
            @ApiImplicitParam(dataType = "string", name = "teacherName", value = "教师姓名", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "登录密码", required = true),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "所属院系", required = true),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = false),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = false)
    })
    public ResultVO<Teachers> addTeacher(Teachers teacher) {
        return teachersService.addTeacher(teacher);
    }
    /**
     * 更新教师信息
     */
    @PostMapping("/updateTeacher")
    @ApiOperation(value = "更新教师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师工号（必须存在）", required = true),
            @ApiImplicitParam(dataType = "string", name = "teacherName", value = "教师姓名", required = false),
            @ApiImplicitParam(dataType = "string", name = "password", value = "登录密码", required = false),
            @ApiImplicitParam(dataType = "string", name = "gender", value = "性别（男/女）", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "所属院系", required = false),
            @ApiImplicitParam(dataType = "integer", name = "age", value = "年龄", required = false),
            @ApiImplicitParam(dataType = "string", name = "phone", value = "联系电话", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "string", name = "email", value = "电子邮箱", required = true, defaultValue = "10")
    })
    public ResultVO<Teachers> updateTeacher(Teachers teacher) {
        return teachersService.updateTeacher(teacher);
    }

    /**
     * 删除教师（根据工号）
     */
    @DeleteMapping("/deleteTeacher")
    @ApiOperation(value = "删除教师")
    @ApiImplicitParam(name = "teacherId", value = "教师工号", required = true, dataType = "string")
    public ResultVO<Teachers> deleteTeacher(String teacherId) {
        return teachersService.deleteTeacher(teacherId);
    }
}
